package cn.koala.key.stock.controller;

import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.stock.domain.dto.InventorySearch;
import cn.koala.key.stock.domain.vo.KeyInfoExcel;
import cn.koala.key.stock.domain.vo.KeyInfoVO;
import cn.koala.key.stock.domain.vo.PrintBarCodeVO;
import cn.koala.key.stock.service.KeyInfoService;
import cn.koala.key.stock.service.KeyInventoryService;
import cn.ehi.authority.annotation.WebAuthentication;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultGenerator;
import cn.seed.common.core.ResultList;
import cn.seed.common.utils.LoggerUtils;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @description:钥匙库存
 * @author 32415
 * @time 2019/04/28
 **/
@RestController
@Api(value = "KeyInventory",tags = "钥匙库存")
public class KeyInventoryController {

    @Autowired
    private KeyInfoService keyInfoService;

    @Autowired
    private KeyInventoryService keyInventoryService;

    @ApiOperation(value = "库存查询")
    @GetMapping("/key-inventory")
    public Result<ResultList<KeyInfoVO>> list(InventorySearch inventorySearch) {
        return keyInventoryService.list(inventorySearch);
    }

    @ApiOperation(value = "挂失")
    @PutMapping("/key-inventory/report-loss")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00032")
    public Result reportLoss(@RequestParam String keyBarCode, @RequestParam String dropReason) {
        return keyInfoService.reportLoss(keyBarCode,dropReason);
    }

    @ApiOperation(value = "入库")
    @PutMapping("/key-inventory/in-stock")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00033")
    public Result inStock(@RequestParam String keyBarCode) {
        return keyInfoService.inStock(keyBarCode);
    }

    @ApiOperation(value = "库存导出功能")
    @GetMapping("/key-inventory/print")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00034")
    public void exportInventory(HttpServletResponse response,
                                InventorySearch inventorySearch){
        Result<List<KeyInfoVO>> list = keyInventoryService.exportInventory(inventorySearch);
        List<KeyInfoVO> data = list.getResult();
        List<KeyInfoExcel> resultList = keyInventoryService.dataProcessing(data);
        //导出操作
        try {
            ExcelUtils.setResponse(response,"库存");
            ExcelUtils.export(response.getOutputStream(),ExcelTypeEnum.XLSX,resultList,KeyInfoExcel.class);
        } catch (Exception e) {
            LoggerUtils.error(KeyInventoryController.class, e.getMessage());
        }
    }

    @ApiOperation(value = "打印条码")
    @GetMapping("/key-inventory/printBarCode")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00031")
    public Result<PrintBarCodeVO> printBarCode(@RequestParam String keyBarCode) {
        return keyInventoryService.printBarCode(keyBarCode);
    }

    @ApiOperation(value = "批量打印条码")
    @GetMapping("/key-inventory/batchPrintBarCode")
    @WebAuthentication(systemCode = "KoalaKey",moduleIds = "KOALAKEY_B_00035")
    public Result<List<PrintBarCodeVO>> printBarCode(@RequestParam Integer startCode,@RequestParam Integer endCode) {
        return keyInventoryService.batchPrintBarCode(startCode,endCode);
    }

}
